import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'viewmodel/AccountingAddViewModel.dart';
import 'viewmodel/AccountingViewModel.dart';
import 'global/GlobalTheme.dart';
import 'viewmodel/LoginViewModel.dart';
import 'viewmodel/ThemeViewModel.dart';
import 'routes/Routes.dart';

import 'global/Global.dart';

final GlobalKey<NavigatorState> navigatorKey =
    new GlobalKey<NavigatorState>(); // 全局的上下文对象
void main() async {
  SharedPreferences.setMockInitialValues({}); // 解决持久化报错
  SharedPreferences sp = await SharedPreferences.getInstance();
  int color = sp.getInt("color") ?? 0;
  ThemeViewModel themeViewModel = ThemeViewModel();
  themeViewModel.setColor(color);
  runApp(
      // MyApp()
      // ChangeNotifierProvider(
      //     create: (context) => LoginViewModel(),
      //   child: MyApp(),
      // ),
      MultiProvider(
    providers: [
      ChangeNotifierProvider(
        create: (context) => LoginViewModel(),
      ),
      ChangeNotifierProvider(
        create: (context) => themeViewModel,
      ),
      ChangeNotifierProvider(
        create: (context) => AccountingViewModel(),
      ),
      ChangeNotifierProvider(
        create: (context) => AccountingAddViewModel(),
      ),
    ],
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    // 适配屏幕包裹
    return ScreenUtilInit(
      designSize: Size(360, 690), // 设计稿尺寸
      builder: () => MaterialApp(
        // debugShowCheckedModeBanner: false, // 隐藏debug
        navigatorKey: navigatorKey,
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: themes[Provider.of<ThemeViewModel>(context).getColor],
        ),
        // ThemeData.light().copyWith(
        //   primaryColor: themes[Provider.of<ThemeViewModel>(context).getColor],
        //   buttonTheme: ButtonThemeData(
        //     buttonColor: themes[Provider.of<ThemeViewModel>(context).getColor],
        //     textTheme: ButtonTextTheme.primary,
        //   ),
        // ),
        // home: MyHomePage(title: 'Flutter Demo Home Page'),
        routes: routes,
        initialRoute: '/',
        builder: EasyLoading.init(), // 使用 EasyLoading
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  // @override
  // void initState() {
  //   super.initState();
  //   // loadData();
  // }

  void loadData() async {
    var res = await Global()
        .getInstance()
        .dio
        .get('dio/dio', queryParameters: {"id": '123'});
    print(res);
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have 11qw:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
